package com.m7788.screen;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

/**
 * Created by gxchen on 2018/7/13.
 */

public class DownPicService extends Service {
    private MyBinder myBinder = new MyBinder();

    public interface OnLoadResultListener {
        void loadSuccess(DrawableTypeRequest<String> request);

        void loadFailed();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        String url = intent.getStringExtra("url");
//        int type = intent.getIntExtra("type",-1);
//        int windowWidth
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void loadStartPage(final String url, int windownWidth, int windowHeight, final OnLoadResultListener onLoadResultListener) {
        final DrawableTypeRequest<String> request = Glide.with(this).load(url);
        request.diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String s, Target<GlideDrawable> target, boolean b) {
                if (onLoadResultListener != null) {
                    onLoadResultListener.loadFailed();
                }
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable glideDrawable, String s, Target<GlideDrawable> target, boolean b, boolean b1) {
                SharedPreferences mSharedPreference = PreferenceManager.getDefaultSharedPreferences(DownPicService.this);
                SharedPreferences.Editor editor = mSharedPreference.edit();
                editor.putBoolean(url, true);
                editor.commit();
                if(onLoadResultListener!=null){
                    onLoadResultListener.loadSuccess(request);
                }
                return false;
            }
        });
        request.downloadOnly(windownWidth, windowHeight);
    }

    public void loadBannerPage(final String url, int windownWidth, int windowHeight, final OnLoadResultListener onLoadResultListener) {
        final DrawableTypeRequest<String> request = Glide.with(this).load(url);
        request.diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String s, Target<GlideDrawable> target, boolean b) {
                if (onLoadResultListener != null) {
                    onLoadResultListener.loadFailed();
                }
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable glideDrawable, final String s, Target<GlideDrawable> target, boolean b, boolean b1) {
                SharedPreferences mSharedPreference = PreferenceManager.getDefaultSharedPreferences(DownPicService.this);
                SharedPreferences.Editor editor = mSharedPreference.edit();
                editor.putBoolean(url, true);
                editor.commit();
                if (onLoadResultListener != null) {
                    onLoadResultListener.loadSuccess(request);
                }
                return false;
            }
        });
        request.downloadOnly(windownWidth, windowHeight);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    public class MyBinder extends Binder {
        public DownPicService getService() {
            return DownPicService.this;
        }
    }
}
